package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.DeleteException;
import com.fantastic.common.exception.sql.InsertException;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.exception.sql.UpdateException;
import com.fantastic.common.utils.FilesUtils;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.mapper.zjs.SpecMapper;
import com.fantastic.mapper.zjs.SpecTypeMapper;
import com.fantastic.mapper.zjs.VarietySpecsMapper;
import com.fantastic.model.pojo.zjs.Spec;
import com.fantastic.model.pojo.zjs.SpecType;
import com.fantastic.model.pojo.zjs.TreeTableData;
import com.fantastic.model.pojo.zjs.VarietySpecs;
import com.fantastic.model.vo.zjs.SpecVo;
import com.fantastic.model.vo.zjs.SpecsTypeVo;
import com.fantastic.model.vo.zjs.VarietySpecsVo;
import com.fantastic.service.zjs.VarietySpecsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/5/31 09:52
 * @description: 菜品信息和菜品规格信息关联表service接口实现类
 */
@Service
public class VarietySpecsServiceImpl implements VarietySpecsService {

    @Resource
    private VarietySpecsMapper varietySpecsMapper;

    @Resource
    private SpecMapper specMapper;
    @Resource
    private SpecTypeMapper specTypeMapper;

    /**
     * 查询
     * 无条件时是查询全部的分页查询
     * 有条件时是查询条件的分页查询
     *
     * @param varietySpecs
     * @return
     */
    @Override
    public List<VarietySpecs> listVarietySpecs(VarietySpecs varietySpecs) {
        List<VarietySpecs> varietySpecsList = varietySpecsMapper.listVarietySpecs(varietySpecs);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), varietySpecsList);
        return varietySpecsList;
    }

    /**
     * 查询单个菜品信息和菜品规格信息关联表信息
     * 通过菜品id和菜品规格信息id 查询变动价格
     *
     * @param varietyId 菜品信息id
     * @param specId    菜品规格信息
     * @return
     */
    @Override
    public VarietySpecs getVarietySpecs(Integer varietyId, Integer specId) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.SELECT_ERROR), varietyId, specId);
        VarietySpecs varietySpecs = varietySpecsMapper.getVarietySpecs(varietyId, specId);
        IsEmptyUtils.objectIsNull(new SelectException(RespStatusInfo.SELECT_NULL), varietySpecs);
        return varietySpecs;
    }

    /**
     * 查询全部或是单个菜品信息和多个规格的关联信息
     *
     * @param varietyId
     * @return
     */
    @Override
    public List<VarietySpecsVo> listVarietySpecsVo(Integer varietyId) {
        List<VarietySpecsVo> varietySpecsVos = varietySpecsMapper.listVarietySpecsVo(varietyId);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), varietySpecsVos);
        for (VarietySpecsVo vo : varietySpecsVos) {
            vo.setVarietyPicture(FilesUtils.getSlashParse(vo.getVarietyPicture()));
            List<SpecsTypeVo> varietySpecsTypeVo = vo.getVarietySpecsTypeVo();
            for (SpecsTypeVo vo2 : varietySpecsTypeVo) {
                List<SpecVo> specVoList = vo2.getSpecVoList();
                for (SpecVo vo3 : specVoList) {
                    if (vo3.getSpecPicture() != null) {
                        vo3.setSpecPicture(FilesUtils.getSlashParse(vo3.getSpecPicture()));
                    }
                }
            }
        }
        return varietySpecsVos;
    }

    /**
     * 查询所有的菜品规格类型和其类型下的具体规格
     *
     * @return
     */
    @Override
    public List<TreeTableData> listSpecsType() {
        List<Spec> specs = specMapper.listSpec(new Spec());
        List<SpecType> specTypes = specTypeMapper.listSpecType(new SpecType());
        List<TreeTableData> treeList = new ArrayList<>();

        for (SpecType type : specTypes) {
            treeList.add(new TreeTableData(type.getSpecTypeId(), type.getSpecTypeName()));
        }

        for (Spec spce : specs) {
            treeList.add(new TreeTableData(spce.getSpecId(), spce.getSpecName(), 1, spce.getSpecTypeId()));
        }

        return treeList;
    }

    /**
     * 添加菜品信息和菜品规格信息关联表信息
     *
     * @param varietySpecs
     * @return
     */
    @Override
    public Integer insertVarietySpecs(VarietySpecs varietySpecs) {
        IsEmptyUtils.integerIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR), varietySpecs.getVarietyId(), varietySpecs.getSpecId());
        if (varietySpecs.getSpecAddPrice() == null) {
            varietySpecs.setSpecAddPrice(new BigDecimal("0"));
        }
        return varietySpecsMapper.insertVarietySpecs(varietySpecs);
    }

    /**
     * 批量添加
     *
     * @param varietySpecs
     * @return
     */
    @Override
    @Transactional
    public Integer addsVarietySpecs(List<VarietySpecs> varietySpecs) {
        IsEmptyUtils.listIsEmpty(new InsertException(RespStatusInfo.DATA_ERROR),
                varietySpecs);
        varietySpecs.forEach(s -> {
            if (s.getSpecAddPrice() == null) {
                s.setSpecAddPrice(new BigDecimal("0"));
            }
        });
        return varietySpecsMapper.addsVarietySpecs(varietySpecs);
    }

    /**
     * 修改菜品信息和菜品规格信息关联表信息
     *
     * @param varietySpecs
     * @return
     */
    @Override
    public Integer updateVarietySpecs(VarietySpecs varietySpecs) {
        IsEmptyUtils.integerIsEmpty(new UpdateException(RespStatusInfo.DATA_ERROR), varietySpecs.getVarietyId(), varietySpecs.getSpecId());
        IsEmptyUtils.objectIsNull(new UpdateException(RespStatusInfo.DATA_ERROR), varietySpecs.getSpecAddPrice());
        return varietySpecsMapper.updateVarietySpecs(varietySpecs);
    }

    /**
     * 删除菜品信息和菜品规格信息关联表信息
     *
     * @param varietyId
     * @param specId
     * @return
     */
    @Transactional
    @Override
    public Integer deleteVarietySpecs(Integer varietyId, Integer specId) {
        IsEmptyUtils.integerIsEmpty(new DeleteException(RespStatusInfo.DATA_ERROR), varietyId, specId);
        return varietySpecsMapper.deleteVarietySpecs(varietyId, specId);
    }

    /**
     * 批量删除菜品信息和菜品规格信息关联表信息
     *
     * @param varietyId
     * @param specId
     * @return
     */
    @Override
    public Integer deletesVarietySpecs(Integer[] varietyId, Integer[] specId) {
        return varietySpecsMapper.deletesVarietySpecs(varietyId, specId);
    }
}
